1 package edu.jiangxin.apktoolbox.utils; 2 3 import org.apache.commons.lang3.ArrayUtils; 4 import org.apache.logging.log4j.LogManager; 5 import org.apache.logging.log4j.Logger; 6 7 import java.io.File; 8 import java.io.IOException; 9 import java.math.BigDecimal; 10 import java.math.BigInteger; 11 import java.math.RoundingMode; 12 import java.util.Set; 13 import java.util.TreeSet; 14 15 public class FileUtils { 16 17 private static final Logger LOGGER = LogManager.getLogger(FileUtils.class.getSimpleName()); 18 19 public static final long ONE_KB = 1024; 20 21 public static final BigInteger ONE_KB_BI = BigInteger.valueOf(ONE_KB); 22 23 public static final BigInteger ONE_MB_BI = ONE_KB_BI.multiply(ONE_KB_BI); 24 25 public static final BigInteger ONE_GB_BI = ONE_KB_BI.multiply(ONE_MB_BI); 26 27 public static final BigInteger ONE_TB_BI = ONE_KB_BI.multiply(ONE_GB_BI); 28 29 public static final BigInteger ONE_PB_BI = ONE_KB_BI.multiply(ONE_TB_BI); 30 31 public static final BigInteger ONE_EB_BI = ONE_KB_BI.multiply(ONE_PB_BI); 32 33 public static String sizeOfInHumanFormat(final File file) { 34 return sizeOfInHumanFormat(file.length()); 35 } 36 37 public static String sizeOfInHumanFormat(final long length) { 38 BigInteger size = BigInteger.valueOf(length); 39 String displaySize; 40 41 if (size.divide(ONE_EB_BI).compareTo(BigInteger.ZERO) > 0) { 42 displaySize = divide(size, ONE_EB_BI) + " EB"; 43 } else if (size.divide(ONE_PB_BI).compareTo(BigInteger.ZERO) > 0) { 44 displaySize = divide(size, ONE_PB_BI) + " PB"; 45 } else if (size.divide(ONE_TB_BI).compareTo(BigInteger.ZERO) > 0) { 46 displaySize = divide(size, ONE_TB_BI) + " TB"; 47 } else if (size.divide(ONE_GB_BI).compareTo(BigInteger.ZERO) > 0) { 48 displaySize = divide(size, ONE_GB_BI) + " GB"; 49 } else if (size.divide(ONE_MB_BI).compareTo(BigInteger.ZERO) > 0) { 50 displaySize = divide(size, ONE_MB_BI) + " MB"; 51 } else if (size.divide(ONE_KB_BI).compareTo(BigInteger.ZERO) > 0) { 52 displaySize = divide(size, ONE_KB_BI) + " KB"; 53 } else { 54 displaySize = size + " bytes"; 55 } 56 return displaySize; 57 } 58 59 public static String lastModifiedInHumanFormat(File file) { 60 long lastModified = file.lastModified(); 61 return DateUtils.millisecondToHumanFormat(lastModified); 62 } 63 64 public static double divide(BigInteger size, BigInteger one_bi) { 65 BigDecimal decimalSize = BigDecimal.valueOf(size.doubleValue()) 66 .divide(BigDecimal.valueOf(one_bi.doubleValue()), 2, RoundingMode.HALF_UP); 67 return decimalSize.doubleValue(); 68 } 69 70 public static String getCanonicalPathQuiet(File file) { 71 if (file == null) { 72 LOGGER.warn("getCanonicalPathQuiet failed: file is null"); 73 return null; 74 } 75 try { 76 return file.getCanonicalPath(); 77 } catch (IOException e) { 78 LOGGER.error("getCanonicalPathQuiet failed: IOException"); 79 return null; 80 } 81 } 82 83 84 public static Set<File> listFiles(final File file, final String[] extensions, final boolean recursive) { 85 Set<File> files = new TreeSet<>(); 86 if (!file.exists()) { 87 LOGGER.error("file does not exist: {}", file.getAbsolutePath()); 88 return files; 89 } 90 91 if (file.isDirectory()) { 92 files.addAll(org.apache.commons.io.FileUtils.listFiles(file, extensions, recursive)); 93 return files; 94 } 95 if (file.isFile()) { 96 String fileName = file.getName(); 97 if (ArrayUtils.isEmpty(extensions)) { 98 files.add(file); 99 return files; 100 } 101 for (String extension : extensions) { 102 if (fileName.endsWith(extension)) { 103 files.add(file); 104 return files; 105 } 106 } 107 } 108 LOGGER.warn("file is not directory or file: {}", file.getAbsolutePath()); 109 return files; 110 } 111 } 112 113